<?php
class News
{
protected $_News;
    public function __construct()
    {
	   $this->_News = new Zend_Db_Table('news');
    }	
    
    
    
    public function getMenu($params)
    {
        $Rows = $this->_News->fetchAll($this->_News->select()->order('Date desc'));
        
    	if (!$Rows) return array();
    	
    	$menuItems = $Rows->toArray();
    	$menuResult = array();
    	foreach ($menuItems as $Item)
    	{
    	    $menuResult[] = array(
    	    				'label'=>$Item['Title'], 
                    	    'route'=>'newsFriendly', 
                    	    'module'=>'news',
                    	    'controller'=>'index',
                    	    'action'=>'view',
    	                    'order' => 0, 
                    	    'params'=>array('title'=>Transliteration::asURLSegment($Item['Title']), 'id'=>$Item['ID'])
    	                    );
    	}
    	
    	return $menuResult;
    }
    
    
    
    public function getAll()
    {
    	 $Rows = $this->_News->fetchAll($this->_News->select()->order('Date desc'));
    	 if ($Rows)
    	 {
    	 	return $Rows->toArray();
    	 }
    	 return array();
    }
    
    
    public function getAllByName($filter)
    {
         $Rows = $this->_News->fetchAll($this->_News->select()->where(' LOWER(Title) LIKE LOWER("%' . mysql_escape_string( $filter ). '%")' )->order('Date'));
         if ($Rows)
         {
            return $Rows->toArray();
         }
         return array();
    }

    public function getLast($Count = 5)
    {
         $Rows = $this->_News->fetchAll($this->_News->select()->order('Date desc')->limit($Count,0));
         if ($Rows)
         {
            return $Rows->toArray();
         }
         return array();
    }
    
    
    public function getPressRelise($Count = 4)
    {
         $Rows = $this->_News->fetchAll($this->_News->select()->where('pressrelise = 1')->order('Date desc')->limit($Count,0));
         if ($Rows)
         {
            return $Rows->toArray();
         }
         return array();
    }
    
    public function setPressRelise($ID, $PressRelise)
    {
        $Row = $this->_News->find($ID)->current();
        $Row->PressRelise = $PressRelise;
        $Row->save();
    }
    
    
    public function setPicture($NewsId,$Picture) 
    {
    	$Row = $this->_News->find($NewsId)->current();
    	$Row->Picture = $Picture;
    	$Row->save();
    }
    
    
     public function setTeaser($NewsId,$Teaser) 
    {
    	$Row = $this->_News->find($NewsId)->current();
    	$Row->Teaser = $Teaser;
    	$Row->save();
    }
    
    
    public function Add($Title,$Text,$Date)
    {
    	$Data = array('Title'=>$Title,'Text'=>$Text); 
    	$Row = $this->_News->createRow($Data);
    	
    	if (!empty($Date))
    	{
    	   $Row->Date = $Date;	
    	} 
    	else
    	{
    		$Row->Date = new Zend_Db_Expr('NOW()');
    	}
    	$Row->save();
    	
    	return $Row->ID; 
    }

    public function Edit($ID,$Title = '',$Text = '',$Date = '')
    {
        $Row = $this->_News->find($ID)->current();
        
        
        if ($Date) $Row->Date = $Date;
        if ($Text) $Row->Text = $Text;
        if ($Title) $Row->Title = $Title;  
        $Row->save();
    }
    
    public function Remove($ID)
    {
    	$Rows = $this->_News->find($ID);
    	if (count($Rows))
    	{
    	   $Rows->current()->delete();    
    	}
    	
    } 
    
    public function getOne($ID)
    {
    	$Row = $this->_News->find($ID)->current();
    	if ($Row) return $Row->toArray();
    	return false;
    }
    
    
    
    public function getSearchIndex()
    {
         $Rows = $this->_News->fetchAll($this->_News->select()->order('Date desc'));
         $result = array();
         if ($Rows)
         {
            foreach ($Rows as $row)
            {
                $route = array('module'=>'news','controller'=>'index','action'=>'view','title'=>Transliteration::asURLSegment($row['Title']),'id'=>$row['ID'],'route'=>'newsFriendly');
                $result[] = array('title'=>$row['Title'], 'text'=>strip_tags($row['Text']), 'teaser'=>mb_substr(strip_tags($row['Text']), 0, strpos(strip_tags($row['Text']), '.')+1), 'image'=>'', 'route'=>serialize($route), 'type'=>'news', 'tag'=>$row['ID'], 'image' );
            }
            
            return $result;
         }
         return array();
    }
    
    public function getRssFeed()
    {
         $router = Zend_Controller_Front::getInstance()->getRouter();
         $Rows = $this->_News->fetchAll($this->_News->select()->order('Date desc'));
         $result = array();
         if ($Rows)
         {
            foreach ($Rows as $row)
            {
                $route = array('module'=>'news','controller'=>'index','action'=>'view','title'=>Transliteration::asURLSegment($row['Title']),'id'=>$row['ID'],'route'=>'newsFriendly');
//                $result[] = array('title'=>$row['Title'], 'text'=>strip_tags($row['Text']), 'teaser'=>mb_substr(strip_tags($row['Text']), 0, strpos(strip_tags($row['Text']), '.')+1), 'image'=>'', 'route'=>serialize($route), 'type'=>'news', 'tag'=>$row['ID'], 'image' );
                                
                
                $result[] = array(
                'title'       => $row['Title'],
                'link'        => $router->assemble($route, 'newsFriendly', false, true),
                'text'        => strip_tags( $row['Text'] ),
                'date'        => $row['Date'],
                
                );
            }
            
            return $result;
         }
         return array();
    }
    
    
}